package work.chenbo.springboot.mongo.service.impl;

import com.mongodb.client.result.UpdateResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import work.chenbo.springboot.mongo.common.FieldTypeConstant;
import work.chenbo.springboot.mongo.dao.mongo.UserMongoRepository;
import work.chenbo.springboot.mongo.entity.User;
import work.chenbo.springboot.mongo.service.UserMongoService;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author ChenBo
 * @className UserMongoServiceImpl
 * @date 2020/11/20
 */
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class UserMongoServiceImpl implements UserMongoService {

    private final UserMongoRepository userMongoRepository;
    private final MongoTemplate mongoTemplate;

    @Override
    public User addUser(User user) {
        return userMongoRepository.save(user);
    }

    @Override
    public User updateUser(User user) {
        Query query = new Query(Criteria.where("id").is(user.getId()));
        Update update = new Update();
        if(StringUtils.isNotBlank(user.getUsername())){
            update.set("username",user.getUsername());
        }
        if(StringUtils.isNotBlank(user.getPassword())){
            update.set("password",user.getPassword());
        }
        if(StringUtils.isNotBlank(user.getEmail())){
            update.set("email",user.getEmail());
        }
        if(StringUtils.isNotBlank(user.getPhone())){
            update.set("phone",user.getPhone());
        }
        if(Objects.nonNull(user.getDelFlag())){
            update.set("delFlag",user.getDelFlag());
        }
        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
        return userMongoRepository.findById(user.getId()).orElse(null);
    }


    private void test(){
        User user = new User();
        Class<? extends User> aClass = user.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for(Field field :declaredFields){
            String name = field.getType().getName();
            switch (name){
                case FieldTypeConstant.INT:
                case FieldTypeConstant.INTEGER_O:
                    break;
                case FieldTypeConstant.DOUBLE_O:
                case FieldTypeConstant.DOUBLE:
                    break;
                case FieldTypeConstant.FLOAT_O:
                case FieldTypeConstant.FLOAT:
                    break;
                case FieldTypeConstant.SHORT_O:
                case FieldTypeConstant.SHORT:
                    break;
                case FieldTypeConstant.STRING:
                default:
                    break;
            }


        }
    }
}
